﻿/*
 * ================================================================================
 *          File: frmShopProfits.cs
 *          Desc:查看会员历史消费记录
 *          Auth:JF
 *          Date:2011-10-10
 *================================================================================
 *          Change History
 *================================================================================
 *          Date            Author          Description
 *          --------        --------        ----------------
 *          10/19           JF              1,修正双击会员显示信息为单击,删除DoubleClick与Clike事件
 * 
 *================================================================================
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.BLL;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmMemberConsumeHistory : frmBase
    {
        public frmMemberConsumeHistory(string strMemberID)
        {
            InitializeComponent();
            this.strMemberID = strMemberID;
        }

        private string strMemberID = string.Empty;

        MemberManagerBLL oMemberManagerBLL = new MemberManagerBLL();
        TreeNode tnMain;

        private void frmMemberConsumeHistory_Load(object sender, EventArgs e)
        {
            tnMain = new TreeNode(strMemberID);
            tnMain.Nodes.Add("None");
            treeDate.Nodes.Add(tnMain);
        }

        private void treeDate_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            int level = e.Node.Level;
            string strYear = string.Empty;
            string strMonth = string.Empty;
            string strDay = string.Empty;
            switch (level)
            {
                case 0:
                    e.Node.Nodes.Clear();
                    DataTable dtYear = oMemberManagerBLL.GetMemberConsumeHistoryYear(strMemberID);
                    for (int i = 0; i < dtYear.Rows.Count; i++)
                    {
                        strYear = dtYear.Rows[i][0].ToString();
                        TreeNode tYear = new TreeNode(strYear);
                        tYear.Nodes.Add("");
                        e.Node.Nodes.Add(tYear);
                    }
                    break;
                case 1:
                    e.Node.Nodes.Clear();
                    strYear = e.Node.Text;
                    DataTable dtMonth = oMemberManagerBLL.GetMemberConsumeHistoryMonth(strMemberID, strYear);
                    for (int i = 0; i < dtMonth.Rows.Count; i++)
                    {
                        strMonth = dtMonth.Rows[i][0].ToString();
                        TreeNode tMonth = new TreeNode(strMonth);
                        tMonth.Nodes.Add("");
                        e.Node.Nodes.Add(tMonth);
                    }
                    break;
                case 2:
                    e.Node.Nodes.Clear();
                    strYear = e.Node.Parent.Text;
                    strMonth = e.Node.Text;
                    DataTable dtDay = oMemberManagerBLL.GetMemberConsumeHistoryDay(strMemberID, strYear, strMonth);
                    for (int i = 0; i < dtDay.Rows.Count; i++)
                    {
                        strDay = dtDay.Rows[i][0].ToString();
                        e.Node.Nodes.Add(strDay);
                    }
                    break;
                default:
                    break;
            }
        }

        private void treeDate_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            int level = e.Node.Level;
            decimal RealAmount = 0;
            DataTable dt = new DataTable();
            string strYear = string.Empty;
            string strMonth = string.Empty;
            string strDay = string.Empty;
            int iType = 3;
            switch (level)
            {
                case 0://all
                    iType = 3;
                    break;
                case 1://year
                    strYear = e.Node.Text;
                    iType = 0;
                    break;
                case 2://month
                    strYear = e.Node.Parent.Text;
                    strMonth = e.Node.Text;
                    iType = 1;
                    break;
                case 3://day
                    strYear = e.Node.Parent.Parent.Text;
                    strMonth = e.Node.Parent.Text;
                    strDay = e.Node.Text;
                    iType = 2;
                    break;
                default:
                    break;
            }
            dt = oMemberManagerBLL.GetMemberConsumeHistory(strMemberID, iType, strYear, strMonth, strDay, out RealAmount);
            int dgvIndex = -1;
            dgvHistory.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dgvIndex = dgvHistory.Rows.Add();
                dgvHistory.Rows[dgvIndex].Cells["Barcode"].Value = dt.Rows[i]["Barcode"];
                dgvHistory.Rows[dgvIndex].Cells["MerchandiseClassName"].Value = dt.Rows[i]["MerchandiseClassName"];
                dgvHistory.Rows[dgvIndex].Cells["MerchandiseName"].Value = dt.Rows[i]["MerchandiseName"];
                dgvHistory.Rows[dgvIndex].Cells["UnitName"].Value = dt.Rows[i]["UnitName"];
                dgvHistory.Rows[dgvIndex].Cells["BuyNumber"].Value = dt.Rows[i]["BuyNumber"];
            }
            this.lblResult.Text = "总消费额：" + FormatManager.FormatDecimal(RealAmount);
        }
    }
}
